#include "db/object.h"
#include "db/connection.h"

#include <wx/msgdlg.h>
#include <wx/dbtable.h>

wxString Object::tableName()
{
	return wxT("object");
}

wxDbTable * Object::defineColumns(
		wxDb * db,
		int & id, wxChar * name, int & sessionID)
{
	wxDbTable * table = new wxDbTable(db, Object::tableName(), 3, wxT(""), !wxDB_QUERY_ONLY, wxT(""));
	int i = 0;
	if (id != -1) {
		table->SetColDefs(i++, wxT("id"), DB_DATA_TYPE_INTEGER, &id,
			SQL_INTEGER, sizeof(id), true, true);
	}
	table->SetColDefs(i++, wxT("name"), DB_DATA_TYPE_VARCHAR, name,
        SQL_C_WXCHAR, sizeof(wxChar) * 255, true, true);
	table->SetColDefs(i++, wxT("session_id"), DB_DATA_TYPE_INTEGER, &sessionID,
        SQL_INTEGER, sizeof(sessionID), true, true);
	return table;
}

vector<Object> Object::getFullList()
{
	return Object::getList(wxT(""));
}

vector<Object> Object::getList(const wxString & whereClause)
{
	wxDb * db = DbConnection::getInstance()->getPointer();
	int id = 0;
	int sessionID = 0;
	wxChar name[255];

	wxDbTable * table = Object::defineColumns(db, id, name, sessionID);
	table->SetWhereClause(whereClause);

	if (!table->Open()) {
		DbConnection::handleError(wxT("TABLE OPEN ERROR: "), table->GetDb());
	}
	if (!table->Query()) {
		DbConnection::handleError(wxT("QUERY ERROR: "), table->GetDb());
	}

	vector<Object> ret;
	while (table->GetNext()) {
		Object item;
		item.ID = id;
		item.name = name;
		item.sessionID = sessionID;
		ret.push_back(item);
	}
	return ret;
}

bool Object::createRecord(const Object & record)
{
	wxDb * db = DbConnection::getInstance()->getPointer();
	wxDbTable * table = new wxDbTable(db, Object::tableName(), 2, wxT(""), !wxDB_QUERY_ONLY, wxT(""));
	wxChar name[255];
	int id = -1;
	int sessionID = record.sessionID;
	wxStrcpy(name, record.name);
	
	table->SetColDefs(0, wxT("name"), DB_DATA_TYPE_VARCHAR, name,
		SQL_C_WXCHAR, sizeof(name), true, true);
	table->SetColDefs(1, wxT("session_id"), DB_DATA_TYPE_INTEGER, &sessionID,
		SQL_INTEGER, sizeof(sessionID), true, true);

	if (!table->Open()) {
		DbConnection::handleError(wxT("TABLE OPEN ERROR: "), table->GetDb());
		return false;
	}

	if (!table->Insert()) {
		DbConnection::handleError(wxT("CREATING ERROR: "), table->GetDb());
		table->GetDb()->RollbackTrans();
		wxDELETE(table);
		return false;
	}
	if (table->GetDb()->CommitTrans()) {
		wxDELETE(table);
		return true;
	}
	return false;
}

bool Object::updateRecord(const Object & record)
{
	return false;
}

bool Object::deleteRecord(const Object & record)
{
	wxDb * db = DbConnection::getInstance()->getPointer();
	wxDbTable * table = new wxDbTable(db, Object::tableName(), 2, wxT(""), !wxDB_QUERY_ONLY, wxT(""));
	if (!table->DeleteWhere(wxT("id = ") + wxString::Format(wxT("%d"), record.ID))) {
		DbConnection::handleError(wxT("DELETION ERROR: "), table->GetDb());
		table->GetDb()->RollbackTrans();
		return false;
	}
	return table->GetDb()->CommitTrans();
}